package com.fwj.portal.web.notice;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.model.PageRequest;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.portal.api.notice.NoticeService;
import com.fwj.portal.entity.notice.Notice;
import com.fwj.user.entity.user.UserBaseInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author: fwj
 * @Data: 2022/4/23 23:51
 * @Version 1.0
 * @Description:
 */

@RestController
@RequestMapping("/notice")
@Validated
public class NoticeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private NoticeService noticeService;

    /**
     * @Description 用户修改接口
     * @Param: jsonObject
     * @Author fwj
     * @return com.fwj.core.util.model.ResponseDTO
     * @Createtime 2021/12/23
     */
    @PostMapping("/pushNotice")
    public JSONObject pushNotice(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            //解析转化数据并转化
            Notice notice = JSONObject.parseObject(JSONObject.toJSONString(jsonObject),Notice.class);
            return noticeService.pushNotice(notice,userBaseInfo);
        } catch (Exception e){
            LOGGER.error(e.toString());
            e.printStackTrace();
            return new ResponseDTO().fail();
        }
    }

    @PostMapping("/queryNoticeList")
    @Valid
    public JSONObject queryNoticeList(@RequestBody JSONObject jsonObject){
        try {
            PageRequest pageRequest = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), PageRequest.class);
            //解析转化数据并转化
            return noticeService.queryNoticeList(pageRequest);
        } catch (Exception e){
            LOGGER.error(e.toString());
            e.printStackTrace();
            return new ResponseDTO().fail();
        }
    }


    @PostMapping("/getNoticeDetails")
    public JSONObject getNoticeDetails(@RequestBody JSONObject jsonObject){
        try {
            //解析转化数据并转化
            Long noticeNumber = Long.parseLong(jsonObject.getString("noticeNumber"));
            return noticeService.getNoticeDetails(noticeNumber);
        } catch (Exception e){
            LOGGER.error(e.toString());
            e.printStackTrace();
            return new ResponseDTO().fail();
        }
    }

    @PostMapping("/editNotice")
    public JSONObject editNotice(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            //解析转化数据并转化
            Notice notice = JSONObject.parseObject(JSONObject.toJSONString(jsonObject),Notice.class);
            return noticeService.editNotice(notice);
        } catch (Exception e){
            LOGGER.error(e.toString());
            e.printStackTrace();
            return new ResponseDTO().fail();
        }
    }

    @PostMapping("/deleteNotice")
    public JSONObject deleteNotice(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            //解析转化数据并转化
            Long noticeId = Long.parseLong(jsonObject.getString("noticeId"));
            return noticeService.deleteNotice(noticeId);
        } catch (Exception e){
            LOGGER.error(e.toString());
            e.printStackTrace();
            return new ResponseDTO().fail();
        }
    }

}
